#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>
#include "tree.h"

using namespace std;

void getView(Tree *root, vector<int> &leftView, vector<int> &rightView)
{
    queue<Tree *> Q;
    Q.push(root);
    leftView.push_back(root->data);
    rightView.push_back(root->data);
    int curLevel = 1, nextLevel = 0;
    while(!Q.empty()) {
	Tree *node = Q.front();
	Q.pop();
	curLevel--;
        if (node->left) {
	    Q.push(node->left);
	    nextLevel++;
	}	
	if (node->right) {
	    Q.push(node->right);
	    nextLevel++;
	}
	if (curLevel == 0) {
	    curLevel = nextLevel;
	    nextLevel = 0;
	//    cout << Q.front()->data << " " << Q.back()->data << " " << nextLevel << endl;
	    if (!Q.empty()) rightView.push_back(Q.back()->data);
	    if (!Q.empty()) leftView.push_back(Q.front()->data);
	}
    }

}

int main(int argc, char **argv)
{
    int input[] = {21,7,5,6,3,2,1,4,6,11,35,56};
    int input2[] = {6,23,43,54,12,54,8,10,11,35,56};
    //int input[] = {7};
    Tree *root = NULL, *root2 = NULL;
    int result;

    for (int i = 0; i < sizeof(input)/sizeof(int); i++) {
        TreeInsert(root, input[i]);
    }

    print_ascii_tree(root);
    vector<int> leftView, rightView;
    getView(root, leftView, rightView);
    for (int i = 0; i < leftView.size(); i++)
	cout << leftView[i] << " ";
    cout << endl;
    for (int i = 0; i < rightView.size(); i++)
	cout << rightView[i] << " ";
    cout << endl;
}
